package com.servlet.weatherincz;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import com.weatherinczbackend.Main;
import com.weatherinczbackend.exceptions.InternalErrorException;
 
/**
 * ConcreteXmlServlet
 * - servlet uset when concrete one year need to be generated to xml
 * - argument year is expected
 * 
 * @author Bc. Michal Stava 255739 <255739@mail.muni.cz>¨
 */

//Specific pattern to call this servlet /xmlGenerator?year=xx
@WebServlet(urlPatterns = {"/xmlGenerator"})
public class ConcreteXmlServlet extends HttpServlet {
 
    /**
     * When called, generate concrete one year and also return it on output.
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException 
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
 
        String year = request.getParameter("year");
        
        try {
            out.println(Main.getXmlAsString(Main.initialize(year, "8859_2", "UTF-8")));
        } catch (InternalErrorException ex) {
            throw new ServletException("There is problem in generating xml.", ex);
        }
    }
}